/**
 * @file models/trade.js 实例交易
 * @author
 */

import { routerRedux } from 'dva/router';
import api from '../api';
import { Modal } from 'antd';
import moment from 'moment';
import { tradeDirection, tradeType } from '../config/dict';
const confirm = Modal.confirm;
const nowDate = moment(new Date()).format('YYYYMMDD');

export default{
	 namespace:"trade",
	 state:{
		 begindate:nowDate,
		 enddate:nowDate,
		 pagesize:15,
		 currentpage:1,
		 table:"iftrade",
		 isRefresh:0,
	 },
	 subscriptions:{
		 setup({dispatch,history}){
			 history.listen(({pathname,query})=>{
			 	// console.log('query>>',query)
					if( pathname === '/achievement/hedge/instance' ){
						if(_.isEmpty(query)){
							dispatch({
								type:'getInstanceList',
								payload: {
									table:"iftrade",
									pagesize:15,
									currentpage:1,
									begindate:nowDate,
									enddate:nowDate
								}
							})
							dispatch({
								type:"setState",
								payload: {
									table:"iftrade",
									pagesize:15,
									currentpage:1,
									begindate:nowDate,
									enddate:nowDate
								}
							})
						}else{
							dispatch({
								type:"setState",
								payload:query
							})
							dispatch({
								type: 'getInstanceList',
								payload: query
							})
						}
					}
			 })
		 }
	 },
	 effects:{
		 // * whetherRequest({payload}, {call, put,select}) {
			//  const state=yield select(state => state.trade)
			// 	const newObj={}
			// 	for(var i in payload){
			// 		newObj[i]=state[i]
			// 	}
     //
			// 	if(!_.isEmpty(payload)){
			// 		yield put({
			// 			type:'getInstanceList',
			// 			payload
			// 		})
			// 	}
			//  },

		 * getInstanceList({ payload: query }, { call, put }) {
			 const response = yield call(api.getInstanceList, query);
			 yield put({type:'clearState'})
			 yield put({
				 type: 'getListSuccess',
				 payload:  response ,
			 });
		 },
		 * getAdd({ payload }, { call, put }) {
			 const { begindate, enddate, ...others } = payload;
			 const response = yield call(api.getAdd, { ...others });
			 yield put({
				 type: 'addSuccess',
				 payload: { response },
			 });
			 yield put({
				 type: 'getInstanceList',
				 payload: {
					 begindate,
					 enddate,
					 table: 'iftrade',
				 },
			 });
		 },
		 * remove({ payload }, { call, put }) {
			 const { begindate, enddate, ...others } = payload;
			 const response = yield call(api.removeIntanceRecord, {...others});
			 yield put({
				 type: 'delSuccess',
				 payload: { response },
			 });
			 yield put({
				 type: 'getInstanceList',
				 payload: {
					 begindate,
					 enddate,
					 table: 'iftrade',
				 },
			 });
		 },
		 *changeRouter({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/achievement/hedge/instance`,
				query:{
					...query,
				}
			}))
		},
		* profitAndlossAccount({ payload }, { call, put }) {
			const response = yield call(api.profitAndlossAccount, payload);
			yield put({
				type: 'plaSuccess',
				payload: { response },
			});
		},
		* saveEdit({ payload }, { call, put }) {
			const response = yield call(api.saveEdit, { ...payload });
			yield put({
				type: 'saveSuccess',
				payload: { response },
			});
			yield put(routerRedux.goBack());
		},
		*onRefresh({payload},{call,put}){
			const response = yield call(api.onRefresh, payload );
			yield put({
				type: 'onRefreshSuccess',
				payload:response ,
			});
		},
	},
	 reducers:{
	 	clearState(state) {
			return{
				...state,
				tradelist:[]
			}
		},
		 setState(state, {payload}) {
			//  console.log("trade>>>>1>",state,payload);
				return {
					...state,
					...payload
				}
			},
			plaSuccess(state, action) {
				const { payload: { response } } = action;
				const { msg } = response.msg;
				Modal.info({
					title: '提示信息',
					content: response.msg,
					okText: '确认',
					cancelText: '取消',
				});
				return {
					...state,
				};
			},
			delSuccess(state, action) {
				const { payload: { response } } = action;
				return {
					...state,
				};
			},
		 getListSuccess(state, {payload}) {
			 const {data,page} = payload;
			 data.forEach((item) => {
				item.tradeDirectionText = tradeDirection[item.tradeDirection];
				item.tradeTypeText = tradeType[item.tradeType];
				item.tradeDay = moment(item.tradeDay).format('YYYYMMDD');
			});
			 const status=data ? true:false
				return {
					...state,
					status,
					tradelist:data,
					page,
				}
		 },
		 onRefreshSuccess(state,{payload}){
		 	// console.log('onRefreshSuccess>>',payload)
		 		const {data,page} = payload;
		 		data.forEach((item) => {
					item.tradeDirectionText = tradeDirection[item.tradeDirection];
					item.tradeTypeText = tradeType[item.tradeType];
					item.tradeDay = moment(item.tradeDay).format('YYYYMMDD');
				});
		 		return {
					...state,
					refreshList:data,
				}
		 },
		 addSuccess(state, action) {
			 const { payload: { response } } = action;
			 return {
				 ...state,
			 };
		 },
		 saveSuccess(state, action) {
			 const { payload: { response } } = action;
			 return {
				 ...state,
			 };
		 },
	 }
 }
